package cn.ljy.authority.service.impl;

import cn.ljy.authority.model.SysDictItem;
import cn.ljy.authority.model.query.SysDictItemQuery;
import cn.ljy.authority.service.SysDictItemService;
import cn.ljy.common.exception.model.MyWebException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.io.Serializable;

@Service
//@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor= MyRollBackException.class)
@Validated//表示开启sprint的校检框架，会自动扫描方法里的@Valid（@Valid注解一般写在接口即可）
@Slf4j
public class ISysDictItemService extends IBaseService<SysDictItem, SysDictItemQuery> implements SysDictItemService {

    @Override
    protected void beforeGet(Serializable id) {

    }

    /**
     * 在添加和编辑前的统一验证
     * @param sysDictItem
     */
    private void validateSaveOrUpdate(SysDictItem sysDictItem){
        //判断是否选择所属字典
        if(sysDictItem.getSysDict()==null&&sysDictItem.getSysDict().getId()==null){
            throw new MyWebException("操作失败：字典列表项必须有所属的字典");
        }
    }

    @Override
    protected void beforeUpdate(SysDictItem sysDictItem) {
        //基本验证
        validateSaveOrUpdate(sysDictItem);
    }

    @Override
    protected void beforeSave(SysDictItem sysDictItem) {
        //基本验证
        validateSaveOrUpdate(sysDictItem);
    }

    @Override
    protected void beforeDelete(Serializable[] ids) {

    }
}
